package com.project.frba.bifront.server.rpc;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.project.frba.bi.descriptor.Descriptor;
import com.project.frba.bi.descriptor.DescriptorsLoader;
import com.project.frba.bi.descriptor.Property;
import com.project.frba.bifront.client.model.FrontDescriptor;
import com.project.frba.bifront.client.model.format.CurrencyFormatter;
import com.project.frba.bifront.client.model.format.Formatter;
import com.project.frba.bifront.client.model.format.IntegerFormatter;
import com.project.frba.bifront.client.rpc.FrontGeneralServices;

public class FrontGeneralServicesImpl extends RemoteServiceServlet implements FrontGeneralServices{

	private static final long serialVersionUID = -6635046507862245524L;

	public Collection<FrontDescriptor> getDescriptors() {
		DescriptorsLoader loader = new DescriptorsLoader();
		loader.loadDescriptors();
		List<FrontDescriptor> frontDescriptors = new ArrayList<FrontDescriptor>();
		String userPrivilege = this.getThreadLocalRequest().getSession().getAttribute("privilege").toString();
		for (Descriptor descriptor : loader.getAllDescriptors()) {
			if(descriptor.hasPermission(userPrivilege)){
				FrontDescriptor fDescriptor = new FrontDescriptor();
				fDescriptor.setDescriptorName(descriptor.getDescriptorName());
				fDescriptor.setLabel(descriptor.getLabel());
				fDescriptor.setFormatter(getFormatter(descriptor.getFormatterClassName(), descriptor.getCurrencyType()));
				fDescriptor.setFilePath(descriptor.getFilePath());
				for (Property property : descriptor.getProperties()) {
					fDescriptor.addProperty(property.getName(), property.getLabel());
				}
				for (Property property : descriptor.getColumnDefaultProperties()) {
					fDescriptor.addColumnDefaultProperty(property.getName());
				}
				for (Property property : descriptor.getRowDefaultProperties()) {
					fDescriptor.addRowDefaultProperty(property.getName());
				}
				
				frontDescriptors.add(fDescriptor);
			}
		}
		return frontDescriptors;
	}
	
	/**
	 * Toma por defecto el IntegerFormatter;
	 * @param className
	 */
	private Formatter getFormatter(String className, Integer currencyType) {
		try {
			Class<?> formatterClass = Class.forName(className);
			if(currencyType != null){
				CurrencyFormatter currencyFormatter = (CurrencyFormatter)formatterClass.newInstance();
				currencyFormatter.setSelectedSymbol(currencyType);
				return currencyFormatter;
			} else {
				return (Formatter)formatterClass.newInstance();				
			}
		} catch (Exception e) {
			return  new IntegerFormatter();
		}		
	}

	
	public Boolean removeDescriptor(String descriptorName) {
		File file = new File(DescriptorsLoader.getDescriptorByName(descriptorName).getFilePath());
		if(file.exists()){
			file.delete();
			DescriptorsLoader.reload();
			return true;
			
		}
		return false;
	}
	
	
	
	

	

	
}
